<?php

namespace App\Console\Commands;

use App\Models\Base\LogUserRetained;
use App\Service\AccountService;

class FetchAccount extends CommandBase
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'fetch:account {dateTime?}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '统计登录用户';

    /**
     * Create a new command instance.
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $dateTime = $this->argument('dateTime');
        if ($dateTime) {
            $this->today = $dateTime;
            $this->todayTimestamp = strtotime($dateTime);
            $this->yesterdayTimestamp = $this->todayTimestamp - 86400;
            $this->yesterday = date('Y-m-d', $this->yesterdayTimestamp);
        }
        //获取当日登录用户
        $users = AccountService::loginNumber($this->yesterdayTimestamp, $this->todayTimestamp);
        if (!$users) {
            return false;
        }
        $users = $users->toArray();
        //写入本地
        foreach ($users as $key => $value) {
            $value['date_time'] = $this->yesterday;
            $value['created_at'] = $value['updated_at'] = $this->today;
            LogUserRetained::insertIgnore($value);
        }
    }
}
